﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace VPN
{
    public partial class CreateUser : MPage
    {


        protected void btnCreate_Click(object sender, EventArgs e)
        {
            string pass = System.Math.Abs(Guid.NewGuid().GetHashCode()).ToString().Substring(0, 6);
            DateTime exp = getExpireDate(txtLenth.Text.Trim(), ddlUnit.SelectedValue);
            System.Security.Principal.WindowsImpersonationContext wic =
                 ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
            VPN.Querier.UserHelper.AddUser(txtUsername.Text.Trim(),
                new string[] { ddlMenu.SelectedValue, "VPN" },
                pass, exp,
                txtUsername.Text.Trim());
            wic.Undo();

            ltrAccount.Text = txtUsername.Text;
            ltrPassowrd.Text = pass;
            ltrExpire.Text = string.Format("{0}&nbsp;&nbsp;{1}", exp.ToLongDateString(), exp.ToLongTimeString());
            ltrMenu.Text = ddlMenu.SelectedItem.Text;
        }

        private DateTime getExpireDate(string length, string unit)
        {
            int len = Convert.ToInt32(length);
            switch (unit)
            {
                case "月":
                    return DateTime.Now.AddMonths(len);
                case "日":
                    return DateTime.Now.AddDays(len);
                case "小时":
                    return DateTime.Now.AddHours(len);
                default:
                    return DateTime.Now;
            }
        }

    }

}
